import { getDevicesStates, selectForCloud } from '@/api/device'
import { mergeDevicesStates } from '@/utils/device'
import { getArrayItem, setArrayItem, convertArr2Obj } from '@/utils'
const prefix = 'intelirri-'

const getDefaultState = () => {
  return {
    currentDevice: getArrayItem(`${prefix}currentDevice`),
    devicesObj: getArrayItem(`${prefix}devicesObj`),
    devices: getArrayItem(`${prefix}devices`),
    project: getArrayItem(`${prefix}project`),
    projects: getArrayItem(`${prefix}projects`),
    topics: getArrayItem(`${prefix}topics`),
    currentDeviceShow: false,
    tuliShow: getArrayItem('tuliShow', 1),
    cells: getArrayItem(`${prefix}cells`),
    deviceSingle: {},
    rtk: { a: '', b: '', rtk: '', dir: '' }
  }
}

const state = getDefaultState()

const mutations = {
  SET_RTK: (state, rtk) => {
    state.rtk = rtk
  },
  SET_DEVICE_SINGLE: (state, device) => {
    state.deviceSingle = JSON.parse(JSON.stringify(device))
  },
  SET_DEVICE: (state, device) => {
    const index = state.devices.findIndex((val) => {
      return val.serialno === device.serialno
    })
    state.devices[index] = device
  },
  SET_CURRENT_DEVICE: (state, currentDevice) => {
    state.currentDevice = currentDevice
    // setArrayItem(`${prefix}currentDevice`, currentDevice)
  },
  SET_DEVICES_STORAGE: (state, devices) => {
    setArrayItem(`${prefix}devicesObj`, convertArr2Obj(devices, 'serialno'))
    setArrayItem(`${prefix}devices`, devices)
  },
  SET_DEVICES: (state, devices) => {
    state.devices = devices
    state.devicesObj = convertArr2Obj(devices, 'serialno')
  },
  SET_CELLS: (state, cells) => {
    state.cells = cells
    setArrayItem(`${prefix}cells`, cells)
  },
  SET_PROJECT: (state, project) => {
    setArrayItem(`${prefix}project`, project)
    state.project = project
  },
  SET_PROJECTS: (state, projects) => {
    setArrayItem(`${prefix}projects`, projects)
    state.projects = projects
  },
  SET_CURRENT_DEVICE_SHOW: (state, currentDeviceShow) => {
    state.currentDeviceShow = currentDeviceShow
  },
  SET_TOPICS: (state, topics) => {
    setArrayItem(`${prefix}topics`, topics)
    state.topics = topics
  },
  setTuliShow(state, tuliShow) {
    state.tuliShow = tuliShow
    localStorage.setItem('tuliShow', tuliShow)
  }
}

const actions = {
  getAll({ commit }, params) {
    const project = getArrayItem(`${prefix}project`)
    params = params || {}
    if (project && project.id) {
      params = Object.assign(params, { project_id: project.id })
    }
    const lang = getArrayItem('lang')
    if (params.force || !localStorage.getItem(`${prefix}project`) || state.devices.length === 0) {
      return new Promise((resolve, reject) => {
        selectForCloud(params).then(response => {
          const { data } = response
          // commit('SET_DEVICES_STORAGE', data.devices)
          commit('SET_DEVICES', data.devices)
          commit('SET_PROJECT', data.project)
          commit('SET_PROJECTS', data.projects)
          commit('SET_TOPICS', data.topics)
          commit('SET_CELLS', data.cells)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    } else {
      return new Promise((resolve, reject) => {
        getDevicesStates({ project_id: state.project.id, force: 1 }).then(response => {
          const { data } = response
          const devices = mergeDevicesStates(state.devices, data)
          commit('SET_DEVICES', devices)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
